package lightswitch.server.code.sqlhelper.expression;

import java.util.ArrayList;

import lightswitch.server.code.object.Property;

import lightswitch.server.code.sqlhelper.statement.select.Select;
import lightswitch.server.code.util.StringHelper;

public class ResultTable extends AbstractTable {

	Select _select;
	
	public ResultTable()
	{
		propertyList  = new ArrayList<Property>();
		dataRowList = new ArrayList<>();
		referencesList = new ArrayList<>();
	}
	public ResultTable(String statement)
	{
		propertyList  = new ArrayList<Property>();
		dataRowList = new ArrayList<>();
		referencesList = new ArrayList<>();
		statement = StringHelper.NormalizeString(statement);
		
		//select .... [as] [] name
		
		String selectStatement = StringHelper.GetStringInParenthesis(statement);
		String remainString = statement.substring(selectStatement.length()+2);
		
		_select = new Select(selectStatement,null);
		
		remainString = StringHelper.NormalizeString(remainString);
		String[] temp = remainString.split(" ");
		if(temp.length==2 && temp[0].equals("as"))
		{
			alias = temp[1];
			name = temp[1];
			
		}
		else
			if(temp.length==1)
			{
				alias = temp[0];
				name = temp[0];
			}
			else
			{
				// throw exception
			}
	
	}
	public String toString()
	{
		String t = "";
		t=t+"("+_select.toString()+") as "+alias;
		return t;
	}
	public void InitTable()
	{
		AbstractTable _tb = _select.execute();
		InitTableWithAllLists(_tb);

	}
	
	
}
